package com.ask.service.Impl;

import com.ask.constant.StatusConstant;
import com.ask.dto.SetMealDTO;
import com.ask.dto.SetMealQueryDTO;
import com.ask.entity.Dish;
import com.ask.entity.SetMeal;
import com.ask.entity.SetMealDish;
import com.ask.exception.BaseException;
import com.ask.mapper.DishMapper;
import com.ask.mapper.SetMealDishMapper;
import com.ask.mapper.SetMealMapper;
import com.ask.result.PageResult;
import com.ask.service.SetMealService;
import com.ask.vo.DishItemVO;
import com.ask.vo.DishVO;
import com.ask.vo.SetMealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public void save(SetMealDTO setMealDTO) {
        // 1. update setmeal
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealDTO, setMeal);
        setMealMapper.insert(setMeal);
        // 2. udpate setmeal_dish
        List<SetMealDish> setMealDishes = setMealDTO.getSetMealDishes();
        for (SetMealDish setMealDish : setMealDishes) {
            setMealDish.setSetmealId(setMeal.getId());
        }
        setMealDishMapper.insertBatch(setMealDishes);
    }

    @Override
    public SetMealVO getByIdWithDishes(Long id) {
//        // 1. 查询setmeal
//        SetMeal setMeal = setMealMapper.getById(id);
//        // 2. 查询setmeal-dish
//        List<SetMealDish> setMealDishes = setMealDishMapper.getBySetMealId(id);
//        // 3. 封装setmealvo
//        SetMealVO setMealVO = new SetMealVO();
//        BeanUtils.copyProperties(setMeal, setMealVO);
//        setMealVO.setSetmealDishes(setMealDishes);
        // 多表查询
        SetMealVO setMealVO = setMealMapper.getByIdWithDish(id);
        return setMealVO;
    }

    @Override
    public PageResult pageQuery(SetMealQueryDTO setMealQueryDTO) {
        PageHelper.startPage(setMealQueryDTO.getPage(), setMealQueryDTO.getPageSize());
        Page<SetMealVO> page = setMealMapper.pageQuery(setMealQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void startOrstop(Integer status, Long id) {
        // 如果套餐中有停售中的菜品，禁止起售套餐
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishes = setMealMapper.getBySetMealId(id);
            if (dishes != null && dishes.size() > 0) {
                dishes.forEach(dish -> {
                    if (dish.getStatus().equals(StatusConstant.DISABLE))
                        throw new BaseException("套餐中包含停售的菜品，禁止起售！");
                });
            }
        }

        SetMeal setMeal = new SetMeal();
        setMeal.setId(id);
        setMeal.setStatus(status);
        setMealMapper.update(setMeal);
    }

    /**
     * 修改套餐
     *
     * @param setMealDTO
     */
    @Override
    @Transactional
    public void updateWithDishes(SetMealDTO setMealDTO) {
        // 1. update setmeal
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealDTO, setMeal);
        setMealMapper.update(setMeal);

        // 2. update setmeal_dish
        // 2.1 delete setmeal_dish
        Long setMealId = setMealDTO.getId();
        setMealDishMapper.deleteBysetMealId(setMealId);
        // 2.2 insert into setmeal_dish
        List<SetMealDish> setMealDishes = setMealDTO.getSetMealDishes();
        if (setMealDishes != null && !setMealDishes.isEmpty()) {
            setMealDishes.forEach(item -> {
                item.setSetmealId(setMealId);
            });
            setMealDishMapper.insertBatch(setMealDishes);
        }
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        // 1. delete from setmeal
        // 禁止删除正在出售中的套餐
        ids.forEach(id -> {
            SetMeal setMeal = setMealMapper.getById(id);
            if (setMeal.getStatus().equals(StatusConstant.ENABLE))
                throw new BaseException("禁止删除起售中的套餐");
        });

        // 2. delete setmeal & setmeal_dish
        ids.forEach(id -> {
            setMealMapper.deleteById(id);
            setMealDishMapper.deleteBysetMealId(id);
        });
    }

    @Override
    public List<SetMeal> listByCategoryId(Long categoryId) {
        SetMeal setMeal = SetMeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE).build();
        List<SetMeal> setMealList = setMealMapper.list(setMeal);
        return setMealList;
    }

    @Override
    public List<DishItemVO> getDishesById(Long setMealId) {
        List<DishItemVO> dishItemVOS = setMealDishMapper.getDishesBySetMealId(setMealId);
//        List<SetMealDish> setMealDishList = setMealDishMapper.getBySetMealId(id);
//        List<DishItemVO> dishVOList = new ArrayList<>();
//        setMealDishList.forEach(setMealDish -> {
//            DishItemVO dishVO = new DishItemVO();
//            dishVO.setCopies(setMealDish.getCopies());
//            Dish dish = dishMapper.getById(setMealDish.getDishId());
//            BeanUtils.copyProperties(dish, dishVO);
//            dishVOList.add(dishVO);
//        });
        return dishItemVOS;
    }
}
